//
//  WeiboCellLayout.m
//  WXWeibo
//
//  Created by apple on 16/5/11.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "WeiboCellLayout.h"
#import "WXLabel.h"

@implementation WeiboCellLayout


- (void)setWeibo:(WeiboModel *)weibo {
    
    _weibo = weibo;
    
    
    self.cellHeight += kCellOriginHeight;
    
//    NSDictionary *attribute = @{
//                                NSFontAttributeName : [UIFont systemFontOfSize:16]
//                                };
    
//    CGRect textRect = [_weibo.text boundingRectWithSize:CGSizeMake(kScreenWidth - 20, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:NULL];
    
    CGFloat weiboTextHeight = [WXLabel getTextHeight:16 width:kScreenWidth - 20 text:self.weibo.text];
    
    self.weiboTextFrame = CGRectMake(kSpace, kCellOriginHeight + kSpace, kScreenWidth - 20, weiboTextHeight);
    self.cellHeight += CGRectGetHeight(self.weiboTextFrame) + kSpace * 2 ;
//    self.bottomLineFrame = CGRectMake(0, self.cellHeight - 3, kScreenWidth, 3);
    

    
    CGFloat imgsHeight = [self weiboMultiImagesFrame:self.weiboTextFrame picUrls:self.weibo.pic_urls];
    self.cellHeight += imgsHeight;
    self.bottomLineFrame = CGRectMake(0, self.cellHeight - 3, kScreenWidth, 3);
    //转发元素frame设置

    [self retweetedFrame];
    
    for (NSInteger i = self.weiboImgFrameArr.count; i < kImagesCount; i++) {
        [self.weiboImgFrameArr addObject:[NSValue valueWithCGRect:CGRectZero]];
    }
    
    
}


- (NSMutableArray *)weiboImgFrameArr {
    
    if (_weiboImgFrameArr == nil) {
        _weiboImgFrameArr = [NSMutableArray array];
        
    }
    return _weiboImgFrameArr;
}

- (CGFloat)weiboMultiImagesFrame:(CGRect)imgRectFrame picUrls:(NSArray *)urls {
    
    CGFloat imgX = CGRectGetMinX(imgRectFrame);
    CGFloat imgY = CGRectGetMaxY(imgRectFrame);
    CGFloat imgSize = (CGRectGetWidth(imgRectFrame) - 2 *kImageSpace) / kImagePerLineCount;
    NSInteger row = 0;
    NSInteger column = 0;
    for (NSUInteger i = 0; i < urls.count; i++) {
        
        row = i / kImagePerLineCount;
        column = i % kImagePerLineCount;
        
        CGRect imgFrame = CGRectMake(imgX + column * (imgSize + kImageSpace), imgY + kImageSpace + row * (imgSize + kImageSpace), imgSize, imgSize);
        [self.weiboImgFrameArr addObject:[NSValue valueWithCGRect:imgFrame]];
        
    }
    
    NSInteger line = (urls.count + kImagePerLineCount - 1)/ kImagePerLineCount;
    
    NSInteger imgSpace = MAX(line - 1, 0);
    
    NSInteger imgTextSpace = MIN(1, MAX(0, line));
    
    
    CGFloat imgsHeight = line * imgSize + imgSpace *kImageSpace + imgTextSpace *kSpace;
    return imgsHeight;

}

- (void)retweetedFrame {
    
    self.retweetedRectFrame = CGRectZero;
    self.retweetedTextFrame = CGRectZero;
    
    if (_weibo.retweeted_status != nil) {
        
        self.retweetedRectFrame = CGRectMake(10, CGRectGetMaxY(self.weiboTextFrame) + kSpace, kScreenWidth - 20, 0);
        
//        NSDictionary *attribute = @{
//                                    NSFontAttributeName : [UIFont systemFontOfSize:15]
//                                    };
//        CGRect retextRect = [_weibo.retweeted_status.text boundingRectWithSize:CGSizeMake(kScreenWidth - 40, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:NULL];
        CGFloat retweetedTextHeight = [WXLabel getTextHeight:16 width:kScreenWidth - 20 text:self.weibo.retweeted_status.text];
        
        self.retweetedTextFrame = CGRectMake(20, CGRectGetMaxY(self.retweetedRectFrame) , kScreenWidth - 40, retweetedTextHeight);
        CGFloat rectHeight = retweetedTextHeight + kSpace;
        
        CGFloat reweetImgsHeight = [self weiboMultiImagesFrame:self.retweetedTextFrame picUrls:self.weibo.retweeted_status.pic_urls];
        rectHeight += reweetImgsHeight + kSpace;
        
        self.retweetedRectFrame = CGRectMake(10, CGRectGetMaxY(self.weiboTextFrame)  , kScreenWidth - 20, rectHeight- kImageSpace);
        self.cellHeight += CGRectGetHeight(self.retweetedRectFrame);
        
        self.bottomLineFrame = CGRectMake(0, self.cellHeight - 3, kScreenWidth, 3);
        
        

    }
}

@end
